﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LumiSoft.Net.Mime;
using LumiSoft.Net.POP3.Client;
using Jzy.Model;
using Jzy.Model.Mail;
using System.Data;

using Frame.Lib.Core.Attach;
using Frame.Lib.Common;

namespace Jzy.BLL.Mail
{
    public class POP3Client
    {
        private int _userID;
        private POP3_Client pop3;

        public POP3Client(int userID)
        {
            _userID = userID;
            pop3 = new POP3_Client();
        }

        internal List<WebMailInfo> Receive(List<string> lstMailUIDs, WebMailAccountInfo account)
        {
            List<WebMailInfo> lstMailInfos = new List<WebMailInfo>();

            //开始连接邮箱地址

            Connect(account);

            string strTemp = DateTime.Now.ToString();
            //获取邮件信息列表
            POP3_ClientMessageCollection infos = pop3.Messages;

            foreach (POP3_ClientMessage info in infos)
            {
                //每封Email会有一个在Pop3服务器范围内唯一的Id,检查这个Id是否存在就可以知道以前有没有接收过这封邮件

                if (lstMailUIDs.Contains(info.UID))
                {
                    continue;
                }
                //获取这封邮件的内容 

                byte[] bytes = info.HeaderToByte();


                //解析从Pop3服务器发送过来的邮件信息
                Mime mime = Mime.Parse(bytes);


                WebMailInfo mailInfo = new WebMailInfo();


                mailInfo.Name = string.IsNullOrEmpty(mime.MainEntity.Subject) ? "无标题" : mime.MainEntity.Subject;

                mailInfo.Body = "";//这个时候body是没有的

                mailInfo.BodySize = info.Size;
                mailInfo.UserID = _userID;
                mailInfo.MailGuid = info.UID;
                mailInfo.FolderID = (int)MailSysFolder.收件箱;
                mailInfo.FromName = mime.MainEntity.From.Mailboxes[0].DisplayName;//.ToAddressListString();
                mailInfo.FromAddr = mime.MainEntity.From.Mailboxes[0].EmailAddress;//.ToAddressListString();

                if (mime.MainEntity.To != null)
                {
                    mailInfo.ToAddr = mime.MainEntity.To.ToAddressListString();
                }

                if (mime.MainEntity.Cc != null)
                {
                    mailInfo.CCAddr = mime.MainEntity.Cc.ToAddressListString();
                }

                mailInfo.InputTime = mime.MainEntity.Date;
                if (mailInfo.InputTime.Year < 1000)
                {
                    mailInfo.InputTime = DateTime.Now;
                }
                mailInfo.HasAttachment = mime.MainEntity.HasAttachments;
                mailInfo.Status = (int)MailStatus.UnRead;
                mailInfo.HasDownload = false;
                lstMailInfos.Add(mailInfo);

            }

            DisConnect();
            return lstMailInfos;
        }

        private void Connect(WebMailAccountInfo account)
        {
            pop3.Connect(account.Pop3Server, account.Pop3Port);
            pop3.Authenticate(account.Pop3User, account.Pop3Pwd, false);
        }

        private void DisConnect()
        {
            pop3.Disconnect();
        }

        internal List<AttachInfo> DownMail(WebMailAccountInfo account, string mailGuid, out string body)
        {
            Connect(account);

            Mime mime = GetMime(mailGuid);
            body = "";
            List<AttachInfo> lstAttachs = new List<AttachInfo>();
            AttachBO attachBO = new AttachBO("WebMailAttach");

            if (mime != null)
            {
                if (mime.BodyHtml != null)
                {
                    body = mime.BodyHtml;
                }
                else
                {
                    if (mime.BodyText != null)
                    {
                        body = mime.BodyText;
                    }
                }

                if (mime != null)
                {

                    foreach (MimeEntity attach in mime.Attachments)
                    {

                        string strFileName = attach.ContentDisposition_FileName;
                        if (strFileName == null || strFileName == "")
                        {
                            strFileName = attach.ContentType_Name;
                        }
                        if (strFileName == null || strFileName == "")
                        {
                            strFileName = "无文件名称";
                        }

                        AttachInfo tmp = new AttachInfo();
                        tmp.Name = strFileName;
                        tmp.Type = attach.GetContentType();
                        tmp.Path = attachBO.Save(attach.Data,strFileName);
                        tmp.Size = attach.Data.Length;
                        tmp.InputTime = DateTime.Now;
                        tmp.CreatorID = AppSession.UserID;
                        tmp.CreatorName = AppSession.UserName;

                        lstAttachs.Add(tmp);

                    }
                }
            }
            DisConnect();
            return lstAttachs;
        }

        private Mime GetMime(string mailGuid)
        {
            Mime result = null;
            POP3_ClientMessageCollection infos = pop3.Messages;
            foreach (POP3_ClientMessage info in infos)
            {
                if (info.UID == mailGuid)
                {
                    byte[] bytes = info.MessageToByte();
                    result = Mime.Parse(bytes);
                    break;
                }
            }
            return result;
        }

        //internal Byte[] GetAttachContent(WebMailAccountInfo account,string mailGuid, int index)
        //{
        //    Connect(account);

        //    Mime mime = GetMime(mailGuid);
        //    byte[] content = null;
        //    if (mime != null)
        //    {
        //        if (index < mime.Attachments.Length)
        //        {
        //            content = mime.Attachments[index].Data;
        //        }

        //    }

        //    return content;
        //}
    }
}
